import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'

const departmentList = [
  '计算机学院',
  '外国语学院',
  '数学科学学院',
  '物理学院',
  '管理学院',
  '化学学院',
]
const tableData = [
  {
    id: 1, // 唯一标识（新增/编辑区分用）
    teacherId: 'T001', // 教师编号（业务字段）
    name: '张老师',
    department: '计算机学院',
    username: 'zhang@school.edu',
    password: '111111',
  },
  {
    id: 2,
    teacherId: 'T002',
    name: '李老师',
    department: '外国语学院',
    username: 'li@school.edu',
    password: '111111',
  },
  {
    id: 3,
    teacherId: 'T003',
    name: '王老师',
    department: '数学学院',
    username: 'wang@school.edu',
    password: '111111',
  },
  {
    id: 4,
    teacherId: 'T004',
    name: '赵老师',
    department: '物理学院',
    username: 'zhao@school.edu',
    password: '111111',
  },
  {
    id: 5,
    teacherId: 'T005',
    name: '刘老师',
    department: '管理学院',
    username: 'liu@school.edu',
    password: '111111',
  },
  {
    id: 6,
    teacherId: 'T006',
    name: '陈老师',
    department: '化学学院',
    username: 'chen@school.edu',
    password: '111111',
  },
]

const useTeacherManageStore = defineStore('Teacher', {
  state: () => {
    return {
      departmentList,
      allData: tableData,
      tableData: tableData,
      queryForm: {
        studentId: '',
        name: '',
        department: '',
      },
    }
  },
  actions: {
    // 查询方法
    queryData() {
      this.tableData = this.allData.filter((item) => {
        return (
          (!this.queryForm.studentId ||
            item.teacherId.includes(this.queryForm.studentId)) &&
          (!this.queryForm.name || item.name.includes(this.queryForm.name)) &&
          (!this.queryForm.department ||
            item.department.includes(this.queryForm.department))
        )
      })
      if (this.tableData.length) {
        ElMessage({
          showClose: true,
          message: '为你找到以下数据',
          type: 'success',
        })
        //    console.log(this.tableData.length)
      } else {
        ElMessage({
          showClose: true,
          message: '找不到数据，请检查输入',
          type: 'error',
        })
      }
    },
    // 重置方法
    reset() {
      this.tableData = this.allData
      this.queryForm = { studentId: '', name: '', department: '' }
    },
    // 添加学生方法
    addStudent(student: any) {
      // id 用时间戳模拟唯一性
      student.id = Date.now()
      this.tableData.unshift(student)
    },
    // 编辑学生方法
    updateStudent(student: any) {
      console.log(student)
      const index = this.tableData.findIndex((item) => item.id === student.id)
      if (index !== -1) {
        this.tableData[index] = { ...student }
      }
    },
    // 单条删除
    remove(id: any) {
      // console.log(id)
      this.tableData = this.tableData.filter((item) => item.id !== id)
      this.allData = this.allData.filter((item) => item.id !== id)
    },
    // 批量删除
    removeBatch(ids: any) {
      this.tableData = this.tableData.filter((item) => !ids.includes(item.id))
      this.allData = this.allData.filter((item) => !ids.includes(item.id))
    },
  },
})

export default useTeacherManageStore
